import 'dart:ui';

import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/data/store/config_store.dart';
import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/widgets/app/app_title.dart';
import 'package:bxultimate/widgets/mk_load_image.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';

class PublishIndexPage extends StatefulWidget {
  const PublishIndexPage({Key? key}) : super(key: key);

  @override
  State<PublishIndexPage> createState() => _PublishIndexPageState();
}

class _PublishIndexPageState extends State<PublishIndexPage> {
  final controller = Get.find<PublishIndexController>();
  final state = Get.find<PublishIndexController>().state;

  @override
  Widget build(BuildContext context) {
    return MkPage(
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: TDNavBar(
          useDefaultBack: false,
          backgroundColor: Colors.transparent,
          title: '信息报知',
          rightBarItems: [
            TDNavBarItem(
              iconWidget: MkLoadImage(
                Assets.publishHistory,
                width: 20,
                height: 20,
              ),
              action: () {
                Get.toNamed(RouterName.intellHistory);
              },
            )
          ],
        ),
        body: SingleChildScrollView(
          child: _buildBody(),
        ),
      ),
    );
  }

  _buildBody() {
    return Padding(
      padding: EdgeInsets.all(AppTheme.iDefaultPadding),
      child: Column(
        children: [
          AppTitle(
            title: '情报',
            icon: Assets.publishIntell,
          ),
          Gaps.vGap16,
          _buildIntell(),
          Gaps.vGap16,
          // AppTitle(
          //   title: '工作',
          //   icon: Assets.publishWork,
          // ),
          // Gaps.vGap16,
          // _buildWork(),
        ],
      ),
    );
  }

  _buildIntell() {
    return Container(
      width: Get.width,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(AppTheme.iDefaultRadius),
      ),
      child: Wrap(
        alignment: WrapAlignment.start,
        children: [
          for (int i = 0; i < 9; i++)
            _buildIntellCategoryItem(
                ConfigStore.to.baseFileUrl + state.intellCategoryList[i].icon,
                state.intellCategoryList[i].name, () {
              controller.intellCategoryOnTap(i);
            }),
          _buildIntellCategoryItem(
              'https://minio.sxmbxt.com/static/img/publish/more.png', '更多', () {
            TDPicker.showMultiPicker(Get.context,
                title: '情报类型',
                onConfirm: controller.toPublish,
                data: [
                  [...controller.moreList]
                ]);
          })
        ],
      ),
    );
  }

  _buildIntellCategoryItem(icon, name, onTap) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
          width: (Get.width - AppTheme.iDefaultPadding * 2) / 4,
          padding: EdgeInsets.all(AppTheme.iDefaultPadding),
          child: Column(children: [
            MkLoadImage(
              icon,
              width: 50,
              height: 50,
            ),
            Gaps.vGap8,
            Text(name)
          ])),
    );
  }

  _buildWork() {
    return Container(
      width: Get.width,
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(AppTheme.iDefaultRadius),
      ),
      child: Wrap(
        alignment: WrapAlignment.start,
        children: [
          _buildIntellCategoryItem(
              Assets.publishRwtz,
              '任务通知',
              () {}),
          _buildIntellCategoryItem(
              Assets.publishGzdt,
              '工作动态',
              () {}),
          _buildIntellCategoryItem(
              Assets.publishSjhd,
              '商家活动',
              () {}),
        ],
      ),
    );
  }
}
